#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

void main(int argc, char *argv[]) {
    char ipbuf[20]; // save ip address
    unsigned int ipint; // ip地址的整数形式
    struct in_addr addrstr;
    char *oh = NULL;

    while (1) {
        printf("please input ip address\n");
        fgets(ipbuf, 20, stdin);
        if (strcmp(ipbuf, "exit") == 0) break;
        ipbuf[strlen(ipbuf) - 1] = '\0'; // 去掉fgets输入的换行符
        printf("The binary of ip address is %x\n", inet_addr(ipbuf));
        printf("The host byte of binary ip address is %x\n", inet_network(ipbuf));
        inet_aton(ipbuf, &addrstr);
        oh = (char *)&addrstr;
        printf("The inet_aton result is %x\n", addrstr.s_addr);
        printf("The network byte of ip address is %x:%x:%x:%x\n", (*oh) & 0xff, (*(oh + 1)) & 0xff, (*(oh + 2)) & 0xff, (*(oh + 3)) & 0xff);
        printf("The host id of the ip address is %x\n", inet_lnaof(addrstr));
        printf("The network id of the ip address is %x\n", inet_netof(addrstr));
        addrstr.s_addr++;
        oh = (char *)&addrstr;
        printf("The new ip address is %s\n", inet_ntoa(addrstr));
        printf("The inet_aton result of new ip address is %x\n", addrstr.s_addr);
        printf("The network byte of new ip address is %x:%x:%x:%x\n", (*oh) & 0xff, (*(oh + 1)) & 0xff, (*(oh + 2)) & 0xff, (*(oh + 3)) & 0xff);
        printf("The host id of the new ip address is %x\n", inet_lnaof(addrstr));
        printf("The network id of new ip address is %x\n", inet_netof(addrstr));
    }
}